"""
this file is used to split one file
"""

import os
import random
import time

from arff import parserArffItems

def splitonefile(folder,filename,snum):
    """
    split one file into several files(train and test)
    put under this folder
    split without random
    @param folder : the folder to put the split file
        note:the folder should end with /
    @param filename : the file to be split
    @param snum : split number of file
    """
    #folder = filename[0:filename.rfind("/")+1]
    createfolder(folder,snum)

    arff = parserArffItems(filename)
    itemnum = len(arff['datas'])
    eachpartnum = int(itemnum/snum)
    partnum = int((itemnum + eachpartnum - 1)/eachpartnum)
    #output to each split
    for i in range(partnum):
        beginnum = i * eachpartnum
        endnum = min(itemnum-1,(i+1)*eachpartnum-1)
        createonecase(arff['head'],arff['datas'],beginnum,endnum,folder+str(i)+"/")

def splitrandom(folder,filename,snum):
    """
    get one item random 
    """
    #folder = filename[0:filename.rfind("/")+1]
    createfolder(folder,snum)

    arff = parserArffItems(filename)
    itemnum = len(arff['datas'])
    eachpartnum = int(itemnum/snum)
    partnum = int((itemnum + eachpartnum - 1)/eachpartnum)
    #output to each split
    for i in range(partnum):
        beginnum = i * eachpartnum
        endnum = min(itemnum-1,(i+1)*eachpartnum-1)
        createonecaserandom(arff['head'],arff['datas'],snum,folder+str(i)+"/")

def createfolder(folder,snum):
    """
    @param folder the folder prefix
    """
    try:
        os.mkdir(folder)
    except:
        print folder+" is already made"

    for i in range(snum):
        try:
            os.mkdir(folder+str(i)+"/")
        except:
            print folder+str(i)+"/ is already made"
    
def createonecase(header,items,beginnum,endnum,folderprefix):
    """
    create one case
    @param beginnum the index of begin item
    @param endnum the index of end item
    @param folderprefix the folder to output result
    """
    trainf = open(folderprefix+"train.arff","w")
    testf = open(folderprefix+"test.arff","w")
    
    print>>trainf,header
    print>>testf,header
    
    for i in range(len(items)):
        if i >= beginnum and i <=endnum:
            print>>testf,items[i]
        else:
            print>>trainf,items[i]
        
    trainf.close()
    testf.close()
        
def createonecaserandom(header,items,snum,folderprefix):
    """
    create one case random
    train with (snum-1) / snum
    test with 1 / snum
    
    @param beginnum the index of begin item
    @param endnum the index of end item
    @param folderprefix the folder to output result
    """
    testcount = int(float(1)/snum * len(items))

    testindexs = []
    random.seed(time.time()+random.random())
    while len(testindexs) < testcount:
        i = int(len(items) * random.random())
        if testindexs.count(i) == 0:
            testindexs.append(i)

    trainf = open(folderprefix+"train.arff","w")
    testf = open(folderprefix+"test.arff","w")
    
    print>>trainf,header
    print>>testf,header

    for i in range(len(items)):
        if testindexs.count(i) == 0:
            print>>trainf,items[i]
        else:
            print>>testf,items[i]

    trainf.close()
    testf.close()
        
